package com.xm.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;

/**
 * 手机短信发现
 *
 * @author kwc13
 * @date 2023-12-13 下午 1:16
 */
public class SmsUtil {
    /**
     * 发送短信
     *
     * @param phone 手机号
     * @param code  验证码
     * @return 返回验证码
     */
    public static Integer setSms(String phone, Integer code) throws ClientException {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI5tDKdUpXZAjAtNy7UMao",
                "lzoi4tM1s3YEXwh5F98XfPss5XRz7d");
        IAcsClient client = new DefaultAcsClient(profile);
        //阿里云原有的配置信息我们不需要改动
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //自定义短信信息
        request.putQueryParameter("PhoneNumbers", phone);
        //发送给哪个手机号
        request.putQueryParameter("SignName", "hrms");
        //自己配置的短信签名
        request.putQueryParameter("TemplateCode", "SMS_460665608");
        //自己配置的模板 模版CODE

        //构建一个短信验证码
        HashMap<String, Object> map = new HashMap<>(10000);
        map.put("code", code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        //转换成json字符串

        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.getData());
        //发送至客户端 发送短信结果
        response.getData();
        return code;
    }
}
